﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using System.IO.Ports;
using CCWin.SkinControl;

namespace 生命体征测试仪
{
    public partial class formTimerSet : formBase
    {
        public formTimerSet()
        {
            InitializeComponent();
        }
        private bool Timer_Working = false;//是否没有执行完invoke相关操作              
        private bool Timer_Closing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke
        object TimerSync;
        string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        private void formTimerSet_Load(object sender, EventArgs e)
        {
            TimerSync = new object();
            if (SysConfig.TimerCom==null)
            {
                this.Close();
                return;
            }
            timer1.Enabled = true;
            Timer_Working = false;
            Timer_Closing = false;
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {            
            this.Close();
        }

        private void formTimerSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(SysConfig.TimerCom!=null)
            {
                Timer_Closing = true;
                while (Timer_Working)
                    Application.DoEvents();
                //SysConfig.TimerCom.Close();
                timer1.Enabled = false;
            }
        }
        TimerPacket TimerComProc(byte head,byte com,byte[] data)
        {
            TimerPacket ret = new TimerPacket(head,com);
            byte[] tx;
            if (data == null)
                tx = TimerPacket.MakeTimerPacket(head,com);
            else
                tx = TimerPacket.MakeTimerPacket(head,com, data);
            SysConfig.TimerCom.Write(tx, 0, tx.Length);
            int retry = 5;
            while (retry != 0)
            {
                while (true)
                {
                    try
                    {
                        if (ret.DataPacketed((byte)SysConfig.TimerCom.ReadByte()))
                        {
                            return ret;
                        }
                    }
                    catch// (Exception ex)
                    {                        
                        break;
                    }
                }
                retry--;
            }
            return null;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Timer_Closing)
                return;
            timer1.Enabled = false;
            Timer_Working = true;
            lock (TimerSync)
            {
                this.Invoke((EventHandler)(delegate
                {
                    TimerPacket rx = TimerComProc(0x10, 0x00,null);
                    if (rx != null)
                    {
                        tbDTime.Text = string.Format("20{0:D2}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2} {6}", rx.Data[0], rx.Data[1], rx.Data[2], rx.Data[3], rx.Data[4], rx.Data[5],weekdays[rx.Data[6]]);
                        tbCTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + weekdays[(int)DateTime.Now.DayOfWeek];
                    }
                    rx = TimerComProc(0x10, 0x10, null);
                    if (rx != null)
                    {
                        if (rx.Data[0] == 0x55)
                            tbDoor1.Text = "开";
                        else
                            tbDoor1.Text = "关";
                        if (rx.Data[1] == 0x55)
                            tbDoor2.Text = "开";
                        else
                            tbDoor2.Text = "关";
                        if (rx.Data[2] == 0x55)
                            tbDoor3.Text = "开";
                        else
                            tbDoor3.Text = "关";
                        float t = (rx.Data[4]*100+ rx.Data[5]*10) / 100;
                        if (rx.Data[3] == 0x22)
                            tbT.Text = "-" + t.ToString("F1");
                        else
                            tbT.Text = t.ToString("F1");
                        if (rx.Data[7] == 1)
                        {
                            rb1.Checked = true;
                        }
                        else if (rx.Data[7] == 2)
                        {
                            rb2.Checked = true;
                        }
                        else if (rx.Data[7] == 3)
                        {
                            rb3.Checked = true;
                        }
                    }
                }));
            }
            timer1.Enabled = true;
            Timer_Working = false;
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            lock (TimerSync)
            {
                byte[] time = new byte[7];
                DateTime dt=DateTime.Now;
                time[0] = (byte)(dt.Year%100);
                time[1] = (byte)dt.Month;
                time[2] = (byte)dt.Day;
                time[3] = (byte)dt.Hour;
                time[4] = (byte)dt.Minute;
                time[5] = (byte)dt.Second;
                time[6] = (byte)dt.DayOfWeek;
                TimerPacket rx = TimerComProc(0x20, 0x00, time);
            }
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            cbDaySame2.Checked = false;
            cbDaySame3.Checked = false;
            cbDaySame4.Checked = false;
            cbDaySame5.Checked = false;
            cbDaySame6.Checked = false;
            cbDaySame7.Checked = false;
            lock (TimerSync)
            {                
                try
                {
                    TimerPacket rx = TimerComProc(0x10, 0x20, null);
                    if (rx != null)
                    {
                        for (int i = 0; i < 7; i++)
                        {
                            int k = i * 4;
                            SetTimerControl(i + 1, rx.Data[k], rx.Data[k + 1], rx.Data[k + 2], rx.Data[k + 3]);
                        }
                    }
                    else
                    {
                        MessageBox.Show("读取失败");
                    }
                }
                catch //(System.Exception ex)
                {
                	
                }
                
            }
        }
        void SetTimerControl(int index,byte sh,byte sm,byte eh,byte em)
        {
            UInt32 t=BytesOP.MakeInt(sh,sm,eh,em);
            if (t == 0)
            {
                switch (index)
                {
                    case 1:
                        cbSH1.SelectedIndex = 0;
                        cbSM1.SelectedIndex = 0;
                        cbEH1.SelectedIndex = 0;
                        cbEM1.SelectedIndex = 0;
                        cbDayOn1.Checked = true;
                        cbDayOff1.Checked = false;
                        break;
                    case 2:
                        cbSH2.SelectedIndex = 0;
                        cbSM2.SelectedIndex = 0;
                        cbEH2.SelectedIndex = 0;
                        cbEM2.SelectedIndex = 0;
                        cbDayOn2.Checked = true;
                        cbDayOff2.Checked = false;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = 0;
                        cbSM3.SelectedIndex = 0;
                        cbEH3.SelectedIndex = 0;
                        cbEM3.SelectedIndex = 0;
                        cbDayOn3.Checked = true;
                        cbDayOff3.Checked = false;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = 0;
                        cbSM4.SelectedIndex = 0;
                        cbEH4.SelectedIndex = 0;
                        cbEM4.SelectedIndex = 0;
                        cbDayOn4.Checked = true;
                        cbDayOff4.Checked = false;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = 0;
                        cbSM5.SelectedIndex = 0;
                        cbEH5.SelectedIndex = 0;
                        cbEM5.SelectedIndex = 0;
                        cbDayOn5.Checked = true;
                        cbDayOff5.Checked = false;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = 0;
                        cbSM6.SelectedIndex = 0;
                        cbEH6.SelectedIndex = 0;
                        cbEM6.SelectedIndex = 0;
                        cbDayOn6.Checked = true;
                        cbDayOff6.Checked = false;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = 0;
                        cbSM7.SelectedIndex = 0;
                        cbEH7.SelectedIndex = 0;
                        cbEM7.SelectedIndex = 0;
                        cbDayOn7.Checked = true;
                        cbDayOff7.Checked = false;
                        break;
                }
            }
            else if (t == 0xffffffff)
            {
                switch (index)
                {
                    case 1:
                        cbSH1.SelectedIndex = 0;
                        cbSM1.SelectedIndex = 0;
                        cbEH1.SelectedIndex = 0;
                        cbEM1.SelectedIndex = 0;
                        cbDayOn1.Checked = false;
                        cbDayOff1.Checked = true;
                        break;
                    case 2:
                        cbSH2.SelectedIndex = 0;
                        cbSM2.SelectedIndex = 0;
                        cbEH2.SelectedIndex = 0;
                        cbEM2.SelectedIndex = 0;
                        cbDayOn2.Checked = false;
                        cbDayOff2.Checked = true;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = 0;
                        cbSM3.SelectedIndex = 0;
                        cbEH3.SelectedIndex = 0;
                        cbEM3.SelectedIndex = 0;
                        cbDayOn3.Checked = false;
                        cbDayOff3.Checked = true;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = 0;
                        cbSM4.SelectedIndex = 0;
                        cbEH4.SelectedIndex = 0;
                        cbEM4.SelectedIndex = 0;
                        cbDayOn4.Checked = false;
                        cbDayOff4.Checked = true;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = 0;
                        cbSM5.SelectedIndex = 0;
                        cbEH5.SelectedIndex = 0;
                        cbEM5.SelectedIndex = 0;
                        cbDayOn5.Checked = false;
                        cbDayOff5.Checked = true;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = 0;
                        cbSM6.SelectedIndex = 0;
                        cbEH6.SelectedIndex = 0;
                        cbEM6.SelectedIndex = 0;
                        cbDayOn6.Checked = false;
                        cbDayOff6.Checked = true;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = 0;
                        cbSM7.SelectedIndex = 0;
                        cbEH7.SelectedIndex = 0;
                        cbEM7.SelectedIndex = 0;
                        cbDayOn7.Checked = false;
                        cbDayOff7.Checked = true;
                        break;
                }
            }
            else
            {
                switch (index)
                {
                    case 1:
                        cbSH1.SelectedIndex = sh + 1;
                        cbSM1.SelectedIndex = sm+1;
                        cbEH1.SelectedIndex = eh+1;
                        cbEM1.SelectedIndex = em+1;
                        cbDayOn1.Checked = false;
                        cbDayOff1.Checked = false;
                        break;
                    case 2:
                        cbSH2.SelectedIndex = sh+1;
                        cbSM2.SelectedIndex = sm+1;
                        cbEH2.SelectedIndex = eh+1;
                        cbEM2.SelectedIndex = em+1;
                        cbDayOn2.Checked = false;
                        cbDayOff2.Checked = false;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = sh+1;
                        cbSM3.SelectedIndex = sm+1;
                        cbEH3.SelectedIndex = eh+1;
                        cbEM3.SelectedIndex = em+1;
                        cbDayOn3.Checked = false;
                        cbDayOff3.Checked = false;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = sh+1;
                        cbSM4.SelectedIndex = sm+1;
                        cbEH4.SelectedIndex = eh+1;
                        cbEM4.SelectedIndex = em+1;
                        cbDayOn4.Checked = false;
                        cbDayOff4.Checked = false;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = sh+1;
                        cbSM5.SelectedIndex = sm+1;
                        cbEH5.SelectedIndex = eh+1;
                        cbEM5.SelectedIndex = em+1;
                        cbDayOn5.Checked = false;
                        cbDayOff5.Checked = false;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = sh+1;
                        cbSM6.SelectedIndex = sm+1;
                        cbEH6.SelectedIndex = eh+1;
                        cbEM6.SelectedIndex = em+1;
                        cbDayOn6.Checked = false;
                        cbDayOff6.Checked = false;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = sh+1;
                        cbSM7.SelectedIndex = sm+1;
                        cbEH7.SelectedIndex = eh+1;
                        cbEM7.SelectedIndex = em+1;
                        cbDayOn7.Checked = false;
                        cbDayOff7.Checked = false;
                        break;
                }
            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            lock (TimerSync)
            {
                byte[] s = new byte[28];
                int k = 0;
                if (cbDayOn1.Checked)
                {
                    s[k*4+0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff1.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH1.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM1.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH1.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM1.Text); 
                    }
                    catch
                    {
                        MessageBox.Show("周一定时设定有误，请检查!");
                        return;
                    }
                }
                k = 1;
                if (cbDayOn2.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff2.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH2.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM2.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH2.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM2.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周二定时设定有误，请检查!");
                        return;
                    }
                }
                k = 2;
                if (cbDayOn3.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff3.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH3.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM3.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH3.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM3.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周三定时设定有误，请检查!");
                        return;
                    }
                }
                k = 3;
                if (cbDayOn4.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff4.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH4.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM4.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH4.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM4.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周四定时设定有误，请检查!");
                        return;
                    }
                }
                k = 4;
                if (cbDayOn5.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff5.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH5.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM5.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH5.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM5.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周五定时设定有误，请检查!");
                        return;
                    }
                }
                k = 5;
                if (cbDayOn6.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff6.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH6.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM6.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH6.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM6.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周六定时设定有误，请检查!");
                        return;
                    }
                }
                k = 6;
                if (cbDayOn7.Checked)
                {
                    s[k * 4 + 0] = 0;
                    s[k * 4 + 1] = 0;
                    s[k * 4 + 2] = 0;
                    s[k * 4 + 3] = 0;
                }
                else if (cbDayOff7.Checked)
                {
                    s[k * 4 + 0] = 0xff;
                    s[k * 4 + 1] = 0xff;
                    s[k * 4 + 2] = 0xff;
                    s[k * 4 + 3] = 0xff;
                }
                else
                {
                    try
                    {
                        s[k * 4 + 0] = byte.Parse(cbSH7.Text);
                        s[k * 4 + 1] = byte.Parse(cbSM7.Text);
                        s[k * 4 + 2] = byte.Parse(cbEH7.Text);
                        s[k * 4 + 3] = byte.Parse(cbEM7.Text);
                    }
                    catch
                    {
                        MessageBox.Show("周日定时设定有误，请检查!");
                        return;
                    }
                }
                TimerPacket rx = TimerComProc(0x20, 0x20, s);
            }
        }

        private void cbSH1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn1.Checked = false;
            cbDayOff1.Checked = false;
        }

        private void cbSH2_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn2.Checked = false;
            cbDayOff2.Checked = false;
        }

        private void cbSH3_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn3.Checked = false;
            cbDayOff3.Checked = false;
        }

        private void cbSH4_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn4.Checked = false;
            cbDayOff4.Checked = false;
        }

        private void cbSH5_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn5.Checked = false;
            cbDayOff5.Checked = false;
        }

        private void cbSH6_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn6.Checked = false;
            cbDayOff6.Checked = false;
        }

        private void cbSH7_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDayOn7.Checked = false;
            cbDayOff7.Checked = false;
        }

        private void cbDayOn1_CheckedChanged(object sender, EventArgs e)
        {
            SkinCheckBox cb = (SkinCheckBox)sender;
            if(cb.Checked)
            {
                int i = int.Parse(cb.Name.Substring(cb.Name.Length - 1));
                switch (i)
                {
                    case 1:
                        cbSH1.SelectedIndex = 0;
                        cbSM1.SelectedIndex = 0;
                        cbEH1.SelectedIndex = 0;
                        cbEM1.SelectedIndex = 0;
                        cbDayOn1.Checked = true;
                        cbDayOff1.Checked = false;
                        break;
                    case 2:
                        cbSH2.SelectedIndex = 0;
                        cbSM2.SelectedIndex = 0;
                        cbEH2.SelectedIndex = 0;
                        cbEM2.SelectedIndex = 0;
                        cbDayOn2.Checked = true;
                        cbDayOff2.Checked = false;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = 0;
                        cbSM3.SelectedIndex = 0;
                        cbEH3.SelectedIndex = 0;
                        cbEM3.SelectedIndex = 0;
                        cbDayOn3.Checked = true;
                        cbDayOff3.Checked = false;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = 0;
                        cbSM4.SelectedIndex = 0;
                        cbEH4.SelectedIndex = 0;
                        cbEM4.SelectedIndex = 0;
                        cbDayOn4.Checked = true;
                        cbDayOff4.Checked = false;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = 0;
                        cbSM5.SelectedIndex = 0;
                        cbEH5.SelectedIndex = 0;
                        cbEM5.SelectedIndex = 0;
                        cbDayOn5.Checked = true;
                        cbDayOff5.Checked = false;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = 0;
                        cbSM6.SelectedIndex = 0;
                        cbEH6.SelectedIndex = 0;
                        cbEM6.SelectedIndex = 0;
                        cbDayOn6.Checked = true;
                        cbDayOff6.Checked = false;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = 0;
                        cbSM7.SelectedIndex = 0;
                        cbEH7.SelectedIndex = 0;
                        cbEM7.SelectedIndex = 0;
                        cbDayOn7.Checked = true;
                        cbDayOff7.Checked = false;
                        break;
                }
            }
        }

        private void cbDayOff1_CheckedChanged(object sender, EventArgs e)
        {
            SkinCheckBox cb = (SkinCheckBox)sender;
            if (cb.Checked)
            {
                int i = int.Parse(cb.Name.Substring(cb.Name.Length - 1));
                switch (i)
                {
                    case 1:
                        cbSH1.SelectedIndex = 0;
                        cbSM1.SelectedIndex = 0;
                        cbEH1.SelectedIndex = 0;
                        cbEM1.SelectedIndex = 0;
                        cbDayOn1.Checked = false;
                        cbDayOff1.Checked = true;
                        break;
                    case 2:
                        cbSH2.SelectedIndex = 0;
                        cbSM2.SelectedIndex = 0;
                        cbEH2.SelectedIndex = 0;
                        cbEM2.SelectedIndex = 0;
                        cbDayOn2.Checked = false;
                        cbDayOff2.Checked = true;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = 0;
                        cbSM3.SelectedIndex = 0;
                        cbEH3.SelectedIndex = 0;
                        cbEM3.SelectedIndex = 0;
                        cbDayOn3.Checked = false;
                        cbDayOff3.Checked = true;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = 0;
                        cbSM4.SelectedIndex = 0;
                        cbEH4.SelectedIndex = 0;
                        cbEM4.SelectedIndex = 0;
                        cbDayOn4.Checked = false;
                        cbDayOff4.Checked = true;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = 0;
                        cbSM5.SelectedIndex = 0;
                        cbEH5.SelectedIndex = 0;
                        cbEM5.SelectedIndex = 0;
                        cbDayOn5.Checked = false;
                        cbDayOff5.Checked = true;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = 0;
                        cbSM6.SelectedIndex = 0;
                        cbEH6.SelectedIndex = 0;
                        cbEM6.SelectedIndex = 0;
                        cbDayOn6.Checked = false;
                        cbDayOff6.Checked = true;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = 0;
                        cbSM7.SelectedIndex = 0;
                        cbEH7.SelectedIndex = 0;
                        cbEM7.SelectedIndex = 0;
                        cbDayOn7.Checked = false;
                        cbDayOff7.Checked = true;
                        break;
                }
            }
        }

        private void cbDaySame2_CheckedChanged(object sender, EventArgs e)
        {
            SkinCheckBox cb = (SkinCheckBox)sender;
            if (cb.Checked)
            {
                int i = int.Parse(cb.Name.Substring(cb.Name.Length - 1));
                switch (i)
                {
                    case 2:
                        cbSH2.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM2.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH2.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM2.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn2.Checked = cbDayOn1.Checked;
                        cbDayOff2.Checked = cbDayOff1.Checked;
                        break;
                    case 3:
                        cbSH3.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM3.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH3.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM3.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn3.Checked = cbDayOn1.Checked;
                        cbDayOff3.Checked = cbDayOff1.Checked;
                        break;
                    case 4:
                        cbSH4.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM4.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH4.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM4.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn4.Checked = cbDayOn1.Checked;
                        cbDayOff4.Checked = cbDayOff1.Checked;
                        break;
                    case 5:
                        cbSH5.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM5.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH5.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM5.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn5.Checked = cbDayOn1.Checked;
                        cbDayOff5.Checked = cbDayOff1.Checked;
                        break;
                    case 6:
                        cbSH6.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM6.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH6.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM6.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn6.Checked = cbDayOn1.Checked;
                        cbDayOff6.Checked = cbDayOff1.Checked;
                        break;
                    case 7:
                        cbSH7.SelectedIndex = cbSH1.SelectedIndex;
                        cbSM7.SelectedIndex = cbSM1.SelectedIndex;
                        cbEH7.SelectedIndex = cbEH1.SelectedIndex;
                        cbEM7.SelectedIndex = cbEM1.SelectedIndex;
                        cbDayOn7.Checked = cbDayOn1.Checked;
                        cbDayOff7.Checked = cbDayOff1.Checked;
                        break;
                }
            }
        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            lock (TimerSync)
            {
                SkinButton bt = (SkinButton)sender;
                byte[] time=new byte[1];
                if (bt.Text == "开")
                    time[0] = 1;
                else if (bt.Text == "自动")
                    time[0] = 2;
                else if (bt.Text == "关")
                    time[0] = 3;
                else
                    return;
                TimerPacket rx = TimerComProc(0x20, 0x50, time);
            }
        }
    }
    /// <summary>
    /// 包头
    /// </summary>
    public class TimerPacketHead
    {
        /// <summary>
        /// 包头大小
        /// </summary>
        public static Int32 HEAD_SIZE = 3;
        public byte Head;
        public byte Command;
        public byte Len = 0;
        public byte[] Data;
        public TimerPacketHead()
        {
            Data = new byte[HEAD_SIZE];
        }
    }
    public class TimerPacket
    {
        public TimerPacketHead Header;
        public byte NeedHead;
        public byte[] Data;
        public int RxCount;
        public byte[] RxList;
        public byte NeedCommand;
        public static Int32 BUFFER_SIZE = 256;
        public byte[] AllData;
        public TimerPacket(byte head,byte com)
        {
            NeedHead = head;
            NeedCommand = com;
            Header = new TimerPacketHead();
            RxCount = 0;
            AllData = new byte[256];
        }
//         public TimerPacket()
//         {
//             NeedCommand = 0xff;
//             Header = new TimerPacketHead();
//             RxCount = 0;
//             AllData = new byte[256];
//         }
        public static byte[] MakeTimerPacket(byte head,byte com)
        {
            return MakeTimerPacket(head,com, null);
        }
        public static byte[] MakeTimerPacket(byte head, byte com, byte Data)
        {
            return MakeTimerPacket(head, com, new byte[] { Data });
        }
        public static byte[] MakeTimerPacket(byte head, byte com, byte[] data)
        {
            byte[] txbuffer;
            if (data != null)
            {
                txbuffer = new byte[TimerPacketHead.HEAD_SIZE + data.Length + 1];
                txbuffer[0] = head;
                txbuffer[1] = com;
                txbuffer[2] = (byte)(data.Length+4);
                for (int i = 0; i < data.Length; i++)
                {
                    txbuffer[i + TimerPacketHead.HEAD_SIZE] = data[i];
                }
                txbuffer[TimerPacketHead.HEAD_SIZE + data.Length] = (byte)(0xff - Verify.GetVerify_byteSum(txbuffer, 0, txbuffer.Length - 1));
            }
            else
            {
                txbuffer = new byte[TimerPacketHead.HEAD_SIZE + 1];
                txbuffer[0] = head;
                txbuffer[1] = com;
                txbuffer[2] = 0x04;                
                txbuffer[3] = (byte)(0xff-Verify.GetVerify_byteSum(txbuffer, 0, txbuffer.Length - 1));
            }
            return txbuffer;
        }
        public bool DataPacketed(byte rx)
        {
            //判断读取的字节数+缓冲区已有字节数是否超过缓冲区总大小
            AllData[RxCount++] = rx;
            if (RxCount < (TimerPacketHead.HEAD_SIZE + 1))
            {
                Header.Data[RxCount - 1] = rx;
                if (RxCount == 1)
                {
                    if (Header.Data[0] != NeedHead)
                    {
                        RxCount = 0;
                    }
                }
                else if (RxCount == 2)
                {
                    if (Header.Data[1] != (byte)(NeedCommand + 1))
                    {
                        RxCount = 0;
                    }
                    else
                    {
                        switch (NeedCommand)
                        {
                            case 0x10:
                                Header.Len = 12;
                                Data = new byte[9];
                                break;
                            default:
                                Header.Len = 0;
                                break;
                        }
                    }
                }
                else if (RxCount == 3)
                {
                    if (Header.Len == 0)
                    {
                        if (Header.Data[2] < 4)
                            RxCount = 0;
                        else
                        {
                            Header.Len = Header.Data[2];
                            Data = new byte[Header.Len - 3];
                        }
                    }
                }                
            }
            else
            {
                Data[RxCount - TimerPacketHead.HEAD_SIZE - 1] = rx;
                if (RxCount == Header.Len)
                {
                    byte s1 = (byte)(0xff-Verify.GetVerify_byteSum(AllData, 0, RxCount - 1));
                    if (AllData[RxCount - 1] == s1)
                    {
                        return true;
                    }
                    else
                    {
                        RxCount = 0;
                        return false;
                    }
                }
            }
            return false;
        }
    }
}
